using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GoogleNet
{
    /// <summary>
    /// This is an abstract element and cannot be used directly in a KML file. 
    /// Overlay is the base type for image overlays drawn on the planet surface or on the screen. 
    /// Icon specifies the image to use and can be configured to reload images based 
    /// on a timer or by camera changes. This element also includes specifications 
    /// for stacking order of multiple overlays and for adding color and transparency 
    /// values to the base image. 
    /// </summary>
    public class KmlOverlay : KmlFeature, IKmlElement
    {
        private string tagName = "Overlay";

        #region Color
        private Color color;

        /// <summary>
        /// Color values are expressed in hexadecimal notation, including opacity (alpha) values. 
        /// The order of expression is alpha, blue, green, red (<I>aabbggrr</I>). 
        /// The range of values for any one color is 0 to 255 (00 to ff). 
        /// For opacity, 00 is fully transparent and ff is fully opaque. 
        /// For example, if you want to apply a blue color with 50 percent opacity to an overlay, 
        /// you would specify the following: &lt;color&gt;7fff0000&lt;/color&gt; 
        /// </summary>
        /// <value>The color.</value>
        public Color Color
        {
            get { return color; }
            set { color = value; }
        } 
        #endregion

        #region DrawOrder
        private int drawOrder;

        /// <summary>
        /// This element defines the stacking order for the images in overlapping overlays. 
        /// Overlays with higher drawOrder values are drawn on top of overlays with lower drawOrder values. 
        /// </summary>
        /// <value>The draw order.</value>
        public int DrawOrder
        {
            get { return drawOrder; }
            set { drawOrder = value; }
        }
        
        #endregion

        #region Icon
        private KmlIcon icon;

        /// <summary>
        /// Defines the image associated with the Overlay. 
        /// The href element defines the location of the image to be used as the Overlay. 
        /// This location can be either on a local file system or on a web server. 
        /// If this element is omitted or contains no href, a rectangle is drawn using 
        /// the color and size defined by the ground or screen overlay. 
        /// </summary>
        /// <value>The icon.</value>
        public KmlIcon Icon
        {
            get { return icon; }
            set { icon = value; }
        } 
        #endregion

        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        string IKmlElement.TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public override void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartElement(tagName);

            if (color != null)
                writer.WriteElementString("color", convertColorToKmlColor(color));

            if(drawOrder != 0)
                writer.WriteElementString("drawOrder", drawOrder.ToString());

            icon.GenerateKml(writer);

            writer.WriteEndElement();
        }

        #endregion

        /// <summary>
        /// Converts the .NET color to its KML's hexadecimal representation.
        /// </summary>
        /// <param name="c">The color to convert</param>
        /// <returns></returns>
        private string convertColorToKmlColor(Color c)
        {
            string hexColor = string.Format("{0:x2}", c.A) + 
                                string.Format("{0:x2}", c.B) +
                                string.Format("{0:x2}", c.G) + 
                                string.Format("{0:x2}", c.R);

            return hexColor;
        }
    }
}
